import {
  response,
  errorHandle,
  url
} from './response';

const getToken = () => {
  return wx.getStorageSync('auth');
}

/**配方 */
export default {
  recipeList(data, successCallback) {
    wx.request({
      url: `${url}/recipe/list`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //获取配方性能数据
  getPerformance(data, successCallback) {
    ///recipe/performance
    wx.request({
      url: `${url}/recipe/performance`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //删除配方
  recipeRemove(data, successCallback) {
    wx.request({
      url: `${url}/recipe/remove`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  /**
   * 查询配方详情
   * @param {*} data 
   * @param {*} successCallback 
   */
  recipeId(id, successCallback) {
    wx.request({
      url: `${url}/recipe/${id}`,
      method: 'get',
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
   //添加配方
   recipeAdd(data, successCallback) {
    wx.request({
      url: `${url}/recipe/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
   /**
   * 搜索配方
   * @param {*} data 
   * @param {*} successCallback 
   */
  recipeSearch(data, successCallback) {
    wx.request({
      url: `${url}/recipe/search`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //recipe/update
  recipeUpdate(data, successCallback) {
    wx.request({
      url: `${url}/recipe/update`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //配方状态
  recipeUpdateStatus(data, successCallback) {
    wx.request({
      url: `${url}/recipe/update/status`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
  //添加配方
  recipeAdd(data, successCallback) {
    wx.request({
      url: `${url}/recipe/add`,
      method: 'post',
      data,
      header: {
        'x-user-agent': 'miniprogram',
        'x-auth-token': getToken()
      },
      success(res) {
        response(res, successCallback);
      },
      fail: (res) => {
        errorHandle(res.errMsg);
      }
    })
  },
    //获取配方性能数据
    getPerformance(data, successCallback) {
      ///recipe/performance
      wx.request({
        url: `${url}/recipe/performance`,
        method: 'post',
        data,
        header: {
          'x-user-agent': 'miniprogram',
          'x-auth-token': getToken()
        },
        success(res) {
          response(res, successCallback);
        },
        fail: (res) => {
          errorHandle(res.errMsg);
        }
      })
    }
}